'''
陈超依 19377189
现代程序设计第次作九业
'''
import numpy
import math
import time
from PIL import Image as Im
import glob

from numpy.random.mtrand import rand
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

class random_walk():
    numpy.random.seed(int(time.time()))
    def __init__(self, mu, X_0, sigma2, N : int):
        self.mu = mu
        self.X_t_1 = X_0
        self.sigma2 = sigma2
        self.N = N
        self.times = 0
    
    def __iter__(self):
        return self
    
    def __next__(self):
        if (self.times < self.N):
            self.X_t_1 = self.mu + self.X_t_1 \
                + numpy.random.normal(0, math.sqrt(self.sigma2))
            self.times = self.times + 1
            return self.X_t_1
        else:
            raise StopIteration('超过所设置的N值:{}'.format(self.N))


class zip_random_walk():
    def __init__(self, model : random_walk, num : int):
        self.model = model
        self.num = num
        self.times = 0
    
    def __iter__(self):
        return self

    def __next__(self):
        if (self.times < self.num):
            m = self.model
            self.times = self.times + 1
            temp = random_walk(m.mu, m.X_t_1, m.sigma2, m.N)
            return list(temp)
        else:
            raise StopIteration('超过所设置的num值:{}'.format(self.num))


class FaceDataset():
    def __init__(self, image_path_list : list):
        self.image_path_list = image_path_list
        self.times = 0
        self.max = len(image_path_list)
    
    def __getitem__(self, index):
        path = self.image_path_list[index]
        img = Im.open(path)
        return numpy.array(img)

    def __iter__(self):
        return self
    
    def __next__(self):
        if (self.times < self.max):
            temp_index = self.times
            self.times = self.times + 1
            return list(self[temp_index])
        else:
            raise StopIteration('超出给定的路径列表，列表长度:{}'\
                .format(self.max))


def main():
    #随机游走测试
    '''
    test = random_walk(0, 0, 1, 5)     #参数依次为mu X-0 sigma^2 N
    Btest = zip_random_walk(test, 10)  #参数为迭代器与对迭代器进行迭代的次数
    for i in Btest:
        print(i)
        print('\n')
    '''
    #图片读取测试
    '''
    list_of_image = glob.glob(r"E:/Py_Programs/week9/originalPics/*/*/*/*/*.jpg")
    test = FaceDataset(list_of_image)
    print(list_of_image[0], test[0])
    print('\n')
    print(list_of_image[10], test[0])
    print('\n')
    print(list_of_image[100], test[0])
    print('\n')
    print(list_of_image[1000], test[0])
    print('\n')
    print(list_of_image[10000], test[0])
    print('\n')
    '''
    #FaceDataset迭代测试
    list_of_image = glob.glob\
        (r"E:/Py_Programs/week9/originalPics/*/*/*/*/*.jpg")
    test = FaceDataset(list_of_image)
    count = 0 
    #count仅用于计数，方便获取图像名以储存ndarry文件，不影响迭代测试
    for data in test:    #实现迭代
        filename = (((list_of_image[count].split('\\'))\
            [-1]).split('.'))[0]
        #此处为获得文件名，对文件路径进行两次截断
        count += 1
        numpy.save("E:/Py_Programs/week9/outputs/{}.npy"\
            .format(filename), data)

if __name__=='__main__':
	main()